package com.prs.crm.action.hr;

import javax.smartcardio.ATR;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;
import org.courser.struts.BaseAction;
import org.courser.ui.PaginationSupport;
import org.hibernate.criterion.DetachedCriteria;

import com.prs.crm.domain.crm.CustomerLevel;
import com.prs.crm.domain.crm.CustomerType;
import com.prs.crm.domain.hr.AttributeType;
import com.prs.crm.service.hr.AttributeTypeService;
import com.prs.crm.service.sys.LogService;

public class AttributeTypeAction extends BaseAction {

	private static final long serialVersionUID = 1L;

	private AttributeType attributeType;
	private AttributeTypeService attributeTypeService;
	private PaginationSupport pagination;
	private Integer[] attributeTypeIds;
	private LogService logService;

	@Action("listAttributeType")
	public String execute() {
		DetachedCriteria criteria = this.getPaginationHelper().createQuery(
				AttributeType.class);
		pagination = this.getPaginationHelper().prepare(pagination);
		this.setPagination(this.getAttributeTypeService().listByPage(criteria,
				pagination));
		return SUCCESS;
	}

	@Action("selectAttributeType")
	public String selectAttributeType() {
		this.execute();
		return SUCCESS;
	}

	@Action("editAttributeType")
	public String edit() {
		return SUCCESS;
	}

	@Action(value = "saveAttributeType", results = {
			@Result(name = "success", type = "redirect", location = "listAttributeType"),
			@Result(name = "error", type = "chain", location = "editAttributeType") })
	public String save() {
		if (attributeType.getName() == null
				|| "".equals(attributeType.getName().trim())) {
			addActionError("名称不能为空");
			return ERROR;
		}
		if (this.getLogService().getSearchName(AttributeType.class,
				attributeType.getName())) {
			this.addActionError("名称已经存在");
			return ERROR;
		}
		this.getAttributeTypeService().save(attributeType);
		return SUCCESS;
	}

	@Action(value = "removeAttributeType", results = {
			@Result(name = "success", type = "redirect", location = "listAttributeType"),
			@Result(name = "error", type = "chain", location = "listAttributeType") })
	public String remove() {
		if (attributeTypeIds == null) {
			this.addActionError("请选择要删除的公共属性类型");
			return ERROR;
		}
		for (Integer id : attributeTypeIds) {
			AttributeType type = this.getAttributeTypeService().get(id);
			if (type.getAttributes().size() != 0) {
				this.addActionError("已有客户引用，不能删除");
				return ERROR;
			}
		}
		this.getAttributeTypeService().batchRemove(this.getAttributeTypeIds());
		return SUCCESS;
	}

	public AttributeType getAttributeType() {
		return attributeType;
	}

	public void setAttributeType(AttributeType attributeType) {
		this.attributeType = attributeType;
	}

	public AttributeTypeService getAttributeTypeService() {
		return attributeTypeService;
	}

	public void setAttributeTypeService(
			AttributeTypeService attributeTypeService) {
		this.attributeTypeService = attributeTypeService;
	}

	public PaginationSupport getPagination() {
		return pagination;
	}

	public void setPagination(PaginationSupport pagination) {
		this.pagination = pagination;
	}

	public Integer[] getAttributeTypeIds() {
		return attributeTypeIds;
	}

	public void setAttributeTypeIds(Integer[] attributeTypeIds) {
		this.attributeTypeIds = attributeTypeIds;
	}

	public LogService getLogService() {
		return logService;
	}

	public void setLogService(LogService logService) {
		this.logService = logService;
	}
}
